<?php

namespace Home\Model;

class UserModel extends CommonModel {

    // 自动验证
    protected $_validate = array(
        array('email', 'require', '邮箱必填！'),
        array('passw', 'require', '密码必填！'),
        array('repassw', 'require', '确认密码必填！'),

        array('email', 'email', '邮箱格式不正确！'),
        array('email','','邮箱已注册！',0,'unique',1),

        array('repassw','passw','确认密码不正确！',0,'confirm'),
    );

    // 自动完成
    protected $_auto = array(
        array('passw','md5',1,'function'),
        array('create_time','time',1,'function'),
        array('update_time','time',3,'function'),
    );

    // signup
    public function dosignup(){
        $ret = array(
            'status' => true,
            'msg' => '跳转中...',
        );

        $rs = $this->create();

        if (!$rs) {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        } else {
            $rs = $this->add($rs);
            if (!$rs) {
                $ret['status'] = false;
                $ret['msg'] = $this->getError();
            }
        }
        return $ret;
    }

    public function login(){

        $this->_validate = array(
            array('email', 'require', '邮箱必填！'),
            array('passw', 'require', '密码必填！'),

            array('email', 'email', '邮箱格式不正确！'),
        );

        $this->_auto = array(
            array('passw','md5',1,'function'),
        );

        $ret = array(
            'status' => true,
            'msg' => '跳转中...',
        );

        $dt = $this->create();

        if (!$dt) {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        } else {
            $rs = $this->where($dt)->find();
            if (empty($rs)) {
                $ret['status'] = false;
                $ret['msg'] = '帐号密码错误！';
            } else {
                $_SESSION['user_id'] = $rs['id'];
            }
        }
        return $ret;
    }

    public function logout(){
        $ret = array(
            'status' => true,
            'msg' => '跳转中...',
        );
        unset($_SESSION['user_id']);
        return $ret;
    }

    public function upPassw(){
        $ret = array(
            'status' => true,
            'msg' => '密码修改成，请重新登录！',
        );
        // now passw check
        $flt = array(
            'id' => $_POST['id'],
            'passw' => md5($_POST['now_passw']),
        );
        $rs = $this->where($flt)->find();
        if (!$rs) {
            $ret['status'] = false;
            $ret['msg'] = '现有密码错误！';
            return $ret;
        }

        // new passw check
        $this->_validate = array(
            array('passw', 'require', '新密码必填！'),
            array('repassw', 'require', '确认密码必填！'),
            array('repassw','passw','确认密码不正确！',0,'confirm'),
        );

        $this->_auto = array(
            array('passw','md5',3,'function'),
            array('update_time','time',3,'function'),
        );
        // new passw set
        $dt = $this->create();

        if (!$dt) {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        } else {
            $rs = $this->save($dt);
            if (!$rs) {
                $ret['status'] = false;
                $ret['msg'] = $this->getError();
            }
        }
        if ($ret['status'] == true) {
            // logout
            unset($_SESSION['user_id']);
        }
        return $ret;
    }

}
